using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using Prime31;
using Rilisoft.MiniJson;
using UnityEngine;

public class ReputationManager : MonoBehaviour
{
	private class SaveCityDistrict
	{
		public string name;

		public string sceneName;

		public bool elite;

		public bool learning;

		public int nextSpawnGoldCount;

		public int maxReputation;

		public int levelAvailability;

		public float timeDescendingReputation;

		public float timeSpawnCash;

		public int cashCount;

		public int levelsCount;

		public List<int> numberToUnlockList = new List<int>();

		public List<int> cashCountList = new List<int>();

		private float _decreasesReputationPerTick;

		public float decreasesReputationPerTick
		{
			get
			{
				return _decreasesReputationPerTick;
			}
			set
			{
				_decreasesReputationPerTick = value;
			}
		}
	}

	private enum DistrictCashState
	{
		conditionsAreNotMet = 0,
		cacheSpawns = 1,
		ready = 2
	}

	private static ReputationManager instance;

	private static string districtReputationKey = "DistrictReputation_";

	private CityDistricSettings cityDistricSettings;

	private List<int> savePointsForMatch = new List<int>();

	private List<SaveCityDistrict> saveCityDistricSettings;

	private float savePeriodDecreasingRep;

	private int saveMinPlayersForAddReputation;

	private float saveRepForKill;

	private float saveReducRepForKill;

	private float saveMinRepForKill;

	private float saveAddRepCoef;

	private float saveDecreaseReputationForCollectingCache;

	private float saveAddCashCoef;

	private float saveAddGoldCoef;

	private float saveGoldSpawnProbability;

	private int saveGoldSpawnCount;

	public static string keyDistrics = "D";

	public static string keyPointsForMatch = "PFM";

	public static string keyPeriodDecreasingRep = "PDR";

	public static string keyMinPlayersForAddReputation = "MPFA";

	public static string keyRepForKill = "RFK";

	public static string keyReducRepForKill = "RDFK";

	public static string keyMinRepForKill = "MRFK";

	public static string keyAddRepCoef = "ARC";

	public static string keyDecreaseReputationForCollectingCach = "DR";

	public static string keyAddCashCoef = "ADRC";

	public static string keyAddGoldCoef = "ADRG";

	public static string keyGoldSpawnProbability = "GSP";

	public static string keyGoldSpawnCount = "GSC";

	private static string keyNextSpawnGold = "NextSpawnGold_";

	private static string keyRepChangedAmount = "RepChangedAmount_";

	private static string keyPrevTimeDecreaseRep = "PrevTimeDecreaseRep_";

	private static string keyDistrictSpawnState = "DistrictSpawnState_";

	private static string keyStartSpawnTime = "StartSpawnTimes_";

	private static string keyDistrictIsConquered = "DistrictIsConquered_";

	private static string keyHaveBeenCaptured = "HaveBeenCaptured_";

	private static string keyHaveBeenLosted = "HaveBeenLosted_";

	private static string keyAddCashValue = "AdditionCashValueForReputation_";

	private static string keyAddGoldValue = "AdditionGoldValueForReputation_";

	private int countMapOpened;

	private int curTime;

	private float timeShift;

	private static string averageDistrictsCapturedTimeKey = "averageDistrictsCapturedTimeKey";

	private static string defaultJson = "{\"D\":[{\"E\":false,\"L\":false,\"TD\":24,\"R\":400,\"TSC\":3,\"LA\":6,\"CC\":250},{\"E\":true,\"L\":false,\"TD\":24,\"R\":500,\"TSC\":4,\"LC\":1,\"N\":[3],\"CCL\":[10]},{\"E\":false,\"L\":false,\"TD\":24,\"R\":500,\"TSC\":3,\"LA\":6,\"CC\":250},{\"E\":false,\"L\":false,\"TD\":36,\"R\":400,\"TSC\":3,\"LA\":3,\"CC\":200},{\"E\":false,\"L\":false,\"TD\":48,\"R\":500,\"TSC\":3,\"LA\":1,\"CC\":150},{\"E\":false,\"L\":true,\"TD\":48,\"R\":500,\"TSC\":3,\"LA\":1,\"CC\":150},{\"E\":false,\"L\":false,\"TD\":48,\"R\":500,\"TSC\":3,\"LA\":1,\"CC\":150},{\"E\":false,\"L\":false,\"TD\":24,\"R\":300,\"TSC\":3,\"LA\":6,\"CC\":250},{\"E\":false,\"L\":false,\"TD\":24,\"R\":400,\"TSC\":3,\"LA\":6,\"CC\":250},{\"E\":false,\"L\":false,\"TD\":36,\"R\":400,\"TSC\":3,\"LA\":3,\"CC\":200},{\"E\":false,\"L\":false,\"TD\":36,\"R\":400,\"TSC\":3,\"LA\":3,\"CC\":200}],\"PFM\":[60,40,20,2],\"PDR\":60,\"MPFA\":6,\"RFK\":0,\"RDFK\":0,\"MRFK\":0,\"ARC\":1,\"DR\":0,\"ADRC\":0.2,\"ADRG\":0.01,\"GSP\":0.15,\"GSC\":3}";

	private bool JsonFileLoaded;

	private IEnumerator download;

	private List<int> _notificationIds = new List<int>();

	private int minTimeToNotify = 10800;

	private int maxNumberNotification = 3;

	public static ReputationManager Instance
	{
		get
		{
			if (instance == null)
			{
				GameObject gameObject = new GameObject();
				gameObject.name = "ReputationManager";
				gameObject.AddComponent<ReputationManager>();
			}
			return instance;
		}
	}

	private int curApproximatedTime
	{
		get
		{
			return GetUnixTime();
		}
	}

	public event Action<string> reputationChanged;

	public void DistrictMapOpen()
	{
		countMapOpened++;
	}

	private float AddReputation(SaveCityDistrict district, float value)
	{
		return 0f;
	}

	private void EventDistrictCaptured(SaveCityDistrict district)
	{
	}

	private void EventBattlesWithBonus(bool value)
	{
	}

	private void EventDistricMapOpen(int cnt)
	{
	}

	private void EventAverageDistrictsCaptured(int cnt)
	{
		if (curApproximatedTime - BCWStorage.Get<int>(averageDistrictsCapturedTimeKey) > 86400)
		{
			BCWStorage.Set(averageDistrictsCapturedTimeKey, curApproximatedTime);
		}
	}

	public float AddReputationForGameEnded(string sceneName, int place, List<int> kills, int playerCount)
	{
		if (!JsonFileLoaded)
		{
			return 0f;
		}
		SaveCityDistrict saveCityDistrict = saveCityDistricSettings.Find((SaveCityDistrict d) => d.sceneName.Equals(sceneName));
		if (saveCityDistrict == null)
		{
			BCWDebug.LogError("Scene " + sceneName + " not found");
			return 0f;
		}
		UpdateReputationDistrict(saveCityDistrict);
		float reputationForGame = GetReputationForGame(place, playerCount);
		reputationForGame += CalculateRepForKill(kills);
		if (DistrictIsConquered(saveCityDistrict))
		{
			reputationForGame *= saveAddRepCoef;
		}
		AddReputation(saveCityDistrict, reputationForGame);
		return reputationForGame;
	}

	public void ClearAllReputation()
	{
		for (int i = 0; i < saveCityDistricSettings.Count; i++)
		{
			SetDistrictConquered(saveCityDistricSettings[i], false);
			SetReputationForDistrict(saveCityDistricSettings[i], 0f);
			SetDistrictCashState(saveCityDistricSettings[i], DistrictCashState.conditionsAreNotMet);
		}
	}

	public float AddReputation(string sceneName, float count)
	{
		if (!JsonFileLoaded)
		{
			return 0f;
		}
		SaveCityDistrict saveCityDistrict = saveCityDistricSettings.Find((SaveCityDistrict d) => d.sceneName.Equals(sceneName));
		if (saveCityDistrict == null)
		{
			BCWDebug.LogError("Scene " + sceneName + " not found");
			return 0f;
		}
		if (!saveCityDistrict.elite && saveCityDistrict.levelAvailability > 25)
		{
			return 0f;
		}
		if (saveCityDistrict.elite)
		{
			UpdateReputation();
		}
		else
		{
			UpdateReputationDistrict(saveCityDistrict);
		}
		if (saveCityDistrict.elite && GetEliteLevel(saveCityDistrict, GetCountConqueredDistrict()) < 0)
		{
			return 0f;
		}
		UpdateReputationDistrict(saveCityDistrict);
		float result = AddReputation(saveCityDistrict, count);
		if (this.reputationChanged != null)
		{
			this.reputationChanged(sceneName);
		}
		return result;
	}

	private float GetReputationForGame(int place, int playerCount)
	{
		if (playerCount < saveMinPlayersForAddReputation)
		{
			if (PhotonNetwork.isMasterClient)
			{
				EventBattlesWithBonus(false);
			}
			return 0f;
		}
		if (PhotonNetwork.isMasterClient)
		{
			EventBattlesWithBonus(true);
		}
		if (place <= 0)
		{
			BCWDebug.LogError("place invalid");
			return 0f;
		}
		if (place > savePointsForMatch.Count)
		{
			return savePointsForMatch[savePointsForMatch.Count - 1];
		}
		return savePointsForMatch[place - 1];
	}

	private float CalculateRepForKill(List<int> kills)
	{
		float num = 0f;
		foreach (int kill in kills)
		{
			float num2 = saveRepForKill;
			for (int i = 0; i < kill; i++)
			{
				num += num2;
				num2 -= saveReducRepForKill;
				if (num2 < saveMinRepForKill)
				{
					num2 = saveMinRepForKill;
				}
			}
		}
		return num;
	}

	private void ParseSettingFromJSON(string text)
	{
		if (JsonFileLoaded)
		{
			return;
		}
		if (text.Equals(string.Empty))
		{
			text = defaultJson;
		}
		Dictionary<string, object> dictionary = Json.Deserialize(text) as Dictionary<string, object>;
		if (dictionary == null)
		{
			BCWDebug.LogError("Cant Deserialize data!");
			return;
		}
		cityDistricSettings.LoadFromResource();
		saveCityDistricSettings.Clear();
		int num = 0;
		try
		{
			savePeriodDecreasingRep = Convert.ToSingle(dictionary[keyPeriodDecreasingRep]) * 60f;
			saveRepForKill = Convert.ToSingle(dictionary[keyRepForKill]);
			saveReducRepForKill = Convert.ToSingle(dictionary[keyReducRepForKill]);
			saveMinRepForKill = Convert.ToSingle(dictionary[keyMinRepForKill]);
			saveAddRepCoef = Convert.ToSingle(dictionary[keyAddRepCoef]);
			object value = 0;
			if (dictionary.TryGetValue(keyDecreaseReputationForCollectingCach, out value))
			{
				saveDecreaseReputationForCollectingCache = Convert.ToSingle(value) / 100f;
			}
			else
			{
				saveDecreaseReputationForCollectingCache = 0f;
			}
			if (dictionary.TryGetValue(keyAddCashCoef, out value))
			{
				saveAddCashCoef = Convert.ToSingle(value);
			}
			else
			{
				saveAddCashCoef = 0f;
			}
			if (dictionary.TryGetValue(keyAddGoldCoef, out value))
			{
				saveAddGoldCoef = Convert.ToSingle(value);
			}
			else
			{
				saveAddGoldCoef = 0f;
			}
			if (dictionary.TryGetValue(keyGoldSpawnProbability, out value))
			{
				saveGoldSpawnProbability = Convert.ToSingle(value);
			}
			else
			{
				saveGoldSpawnProbability = 0.15f;
			}
			if (dictionary.TryGetValue(keyGoldSpawnCount, out value))
			{
				saveGoldSpawnCount = Convert.ToInt32(value);
			}
			else
			{
				saveGoldSpawnCount = 10;
			}
			saveMinPlayersForAddReputation = Convert.ToInt32(dictionary[keyMinPlayersForAddReputation]);
			List<object> list = (List<object>)dictionary[keyDistrics];
			if (cityDistricSettings.list.Count == list.Count)
			{
				foreach (object item in list)
				{
					Dictionary<string, object> dictionary2 = (Dictionary<string, object>)item;
					SaveCityDistrict saveCityDistrict = new SaveCityDistrict();
					saveCityDistrict.name = cityDistricSettings.list[num].name;
					saveCityDistrict.sceneName = cityDistricSettings.list[num].sceneName;
					saveCityDistrict.maxReputation = Convert.ToInt32(dictionary2[CityDistrict.keyMaxReputation]);
					saveCityDistrict.timeSpawnCash = Convert.ToSingle(dictionary2[CityDistrict.keyTimeSpawnCash]) * 3600f;
					saveCityDistrict.timeDescendingReputation = Convert.ToInt32(dictionary2[CityDistrict.keyTimeDescending]);
					saveCityDistrict.decreasesReputationPerTick = (float)saveCityDistrict.maxReputation / (saveCityDistrict.timeDescendingReputation * 3600f / savePeriodDecreasingRep);
					saveCityDistrict.nextSpawnGoldCount = saveGoldSpawnCount;
					saveCityDistrict.elite = (bool)dictionary2[CityDistrict.keyElite];
					saveCityDistrict.learning = (bool)dictionary2[CityDistrict.keyLearning];
					if (saveCityDistrict.elite)
					{
						saveCityDistrict.levelsCount = Convert.ToInt32(dictionary2[CityDistrict.keyLevelsCount]);
						saveCityDistrict.numberToUnlockList.Clear();
						foreach (object item2 in (List<object>)dictionary2[CityDistrict.keyNumberToUnlockList])
						{
							saveCityDistrict.numberToUnlockList.Add(Convert.ToInt32(item2));
						}
						saveCityDistrict.cashCountList.Clear();
						foreach (object item3 in (List<object>)dictionary2[CityDistrict.keyCashCountList])
						{
							saveCityDistrict.cashCountList.Add(Convert.ToInt32(item3));
						}
					}
					else
					{
						saveCityDistrict.levelAvailability = Convert.ToInt32(dictionary2[CityDistrict.keyLevelAvailability]);
						saveCityDistrict.cashCount = Convert.ToInt32(dictionary2[CityDistrict.keyCashCount]);
					}
					saveCityDistricSettings.Add(saveCityDistrict);
					num++;
				}
			}
			else
			{
				BCWDebug.LogError("Number of districts do not match!");
			}
			cityDistricSettings.pointsForMatch.Clear();
			savePointsForMatch.Clear();
			foreach (object item4 in (List<object>)dictionary[keyPointsForMatch])
			{
				cityDistricSettings.pointsForMatch.Add(Convert.ToInt32(item4));
				savePointsForMatch.Add(cityDistricSettings.pointsForMatch[cityDistricSettings.pointsForMatch.Count - 1]);
			}
			JsonFileLoaded = true;
		}
		catch (Exception ex)
		{
			BCWDebug.LogError("Error parse json " + ex);
			Debug.LogError(text);
			BCWDebug.LogError("Parse default Json " + defaultJson);
			ParseSettingFromJSON(defaultJson);
		}
	}

	private int GetCountConqueredDistrict()
	{
		int num = 0;
		foreach (SaveCityDistrict saveCityDistricSetting in saveCityDistricSettings)
		{
			if (!saveCityDistricSetting.elite && DistrictIsConquered(saveCityDistricSetting))
			{
				num++;
			}
		}
		return num;
	}

	private bool DistrictIsConquered(SaveCityDistrict district)
	{
		return BCWStorage.Get<bool>(keyDistrictIsConquered + district.name);
	}

	private void SetDistrictConquered(SaveCityDistrict district, bool value)
	{
		BCWStorage.Set(keyDistrictIsConquered + district.name, value);
	}

	private int GetStartSpawnTime(SaveCityDistrict district)
	{
		return BCWStorage.Get<int>(keyStartSpawnTime + district.name);
	}

	private void SetStartSpawnTime(SaveCityDistrict district, int time)
	{
		BCWStorage.Set(keyStartSpawnTime + district.name, time);
	}

	private float GetReputationForDistrict(SaveCityDistrict district)
	{
		return BCWStorage.Get<float>(districtReputationKey + district.name);
	}

	private void SetReputationForDistrict(SaveCityDistrict district, float rep)
	{
		BCWStorage.Set(districtReputationKey + district.name, rep);
	}

	private float GetAddCashValueForDistrict(SaveCityDistrict district)
	{
		return BCWStorage.Get<float>(keyAddCashValue + district.name);
	}

	private void SetAddCashValueForDistrict(SaveCityDistrict district, float value)
	{
		BCWStorage.Set(keyAddCashValue + district.name, value);
	}

	private float GetAddGoldValueForDistrict(SaveCityDistrict district)
	{
		return BCWStorage.Get<float>(keyAddGoldValue + district.name);
	}

	private void SetAddGoldValueForDistrict(SaveCityDistrict district, float value)
	{
		BCWStorage.Set(keyAddGoldValue + district.name, value);
	}

	private DistrictCashState GetDistrictCashState(SaveCityDistrict district)
	{
		return (DistrictCashState)BCWStorage.Get<int>(keyDistrictSpawnState + district.name);
	}

	private void SetDistrictCashState(SaveCityDistrict district, DistrictCashState value)
	{
		BCWStorage.Set(keyDistrictSpawnState + district.name, (int)value);
	}

	private SaveCityDistrict GetSettingsDistrict(string name)
	{
		return saveCityDistricSettings.Find((SaveCityDistrict d) => d.name.Equals(name));
	}

	private SaveCityDistrict GetSettingsDistrictForScene(string nameScene)
	{
		return saveCityDistricSettings.Find((SaveCityDistrict d) => d.sceneName.Equals(nameScene));
	}

	private void StartSpawnCash(SaveCityDistrict district)
	{
		int num = curApproximatedTime;
		if ((float)(num - GetStartSpawnTime(district)) < district.timeSpawnCash)
		{
			num = (int)((float)GetStartSpawnTime(district) + district.timeSpawnCash);
		}
		if (GetNextSpawnGold(district) || GetHaveBeenCaptured(district))
		{
			SetNextSpawnGold(district, false);
		}
		else
		{
			SetNextSpawnGold(district, UnityEngine.Random.value < saveGoldSpawnProbability);
		}
		if (GetHaveBeenCaptured(district))
		{
			SetDistrictCashState(district, DistrictCashState.ready);
			num -= (int)district.timeSpawnCash;
		}
		else
		{
			SetDistrictCashState(district, DistrictCashState.cacheSpawns);
		}
		SetStartSpawnTime(district, num);
	}

	private void SetNextSpawnGold(SaveCityDistrict district, bool value)
	{
		BCWStorage.Set(keyNextSpawnGold + district.name, value);
	}

	private bool GetNextSpawnGold(SaveCityDistrict district)
	{
		return BCWStorage.Get<bool>(keyNextSpawnGold + district.name);
	}

	private bool GetHaveBeenCaptured(SaveCityDistrict district)
	{
		return BCWStorage.Get<bool>(keyHaveBeenCaptured + district.name);
	}

	private void SetHaveBeenCaptured(SaveCityDistrict district, bool value)
	{
		BCWStorage.Set(keyHaveBeenCaptured + district.name, value);
		if (value)
		{
			SetHaveBeenLosted(district, false);
		}
	}

	private bool GetHaveBeenLosted(SaveCityDistrict district)
	{
		return BCWStorage.Get<bool>(keyHaveBeenLosted + district.name);
	}

	private void SetHaveBeenLosted(SaveCityDistrict district, bool value)
	{
		BCWStorage.Set(keyHaveBeenLosted + district.name, value);
		if (value)
		{
			SetHaveBeenCaptured(district, false);
		}
	}

	private void SetRepChangedAmount(SaveCityDistrict district, float value)
	{
		BCWStorage.Set(keyRepChangedAmount + district.name, value);
	}

	private float GetRepChangedAmount(SaveCityDistrict district)
	{
		return BCWStorage.Get<float>(keyRepChangedAmount + district.name);
	}

	private int GetEliteLevel(SaveCityDistrict district, int conqueredDistrict)
	{
		if (!district.elite)
		{
			BCWDebug.LogError("District " + base.name + " is not elite");
			return -1;
		}
		for (int i = 0; i < district.numberToUnlockList.Count; i++)
		{
			if (conqueredDistrict < district.numberToUnlockList[i])
			{
				return i - 1;
			}
		}
		return district.numberToUnlockList.Count - 1;
	}

	private void Awake()
	{
		instance = this;
		Initialize();
		RemoveNotification();
	}

	private void Initialize()
	{
		cityDistricSettings = ScriptableObject.CreateInstance<CityDistricSettings>();
		saveCityDistricSettings = new List<SaveCityDistrict>();
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		TryDownloadJson();
	}

	public void TryDownloadJson()
	{
		if (!JsonFileLoaded)
		{
			ParseSettingFromJSON(JSONManager.Instance.GetJSON(URLs.Reputation));
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void UpdateReputationDistrict(SaveCityDistrict district)
	{
		if (DistrictIsConquered(district) || district.elite)
		{
			int num = BCWStorage.Get<int>(keyPrevTimeDecreaseRep + district.name);
			if (num == 0)
			{
				num = curApproximatedTime;
			}
			int num2 = curApproximatedTime - num;
			int num3 = (int)((float)num2 / savePeriodDecreasingRep);
			if (num3 > 0)
			{
				AddReputation(district, (float)(-num3) * district.decreasesReputationPerTick);
				BCWStorage.Set(keyPrevTimeDecreaseRep + district.name, (int)((float)num + (float)num3 * savePeriodDecreasingRep));
			}
		}
		if (GetDistrictCashState(district) == DistrictCashState.cacheSpawns && (float)(curApproximatedTime - GetStartSpawnTime(district)) > district.timeSpawnCash)
		{
			SetDistrictCashState(district, DistrictCashState.ready);
		}
	}

	private void UpdateReputation(bool forElite = true)
	{
		foreach (SaveCityDistrict saveCityDistricSetting in saveCityDistricSettings)
		{
			if (!saveCityDistricSetting.elite)
			{
				UpdateReputationDistrict(saveCityDistricSetting);
			}
		}
		if (!forElite)
		{
			return;
		}
		foreach (SaveCityDistrict saveCityDistricSetting2 in saveCityDistricSettings)
		{
			if (saveCityDistricSetting2.elite)
			{
				UpdateReputationDistrict(saveCityDistricSetting2);
			}
		}
	}

	public Dictionary<string, ReputationMapInfo> GetInfo(bool resetFlags = false)
	{
		if (!JsonFileLoaded)
		{
			BCWDebug.LogError("ФАЙЛ НЕ ЗАГРУЖЕН, ОТДАЕМ НАСТРОЙКИ ПО УМОЛЧАНИЮ!");
			return GetDefaultInfo();
		}
		UpdateReputation();
		Dictionary<string, ReputationMapInfo> dictionary = new Dictionary<string, ReputationMapInfo>();
		int playerLevel = 25;
		int countConqueredDistrict = GetCountConqueredDistrict();
		EventAverageDistrictsCaptured(countConqueredDistrict);
		foreach (SaveCityDistrict saveCityDistricSetting in saveCityDistricSettings)
		{
			dictionary.Add(saveCityDistricSetting.sceneName, GetDistrictInfo(saveCityDistricSetting, playerLevel, countConqueredDistrict, resetFlags));
		}
		return dictionary;
	}

	public ReputationMapInfo GetSingleInfo(string nameScene)
	{
		if (!JsonFileLoaded)
		{
			BCWDebug.LogError("ФАЙЛ НЕ ЗАГРУЖЕН, ОТДАЕМ НАСТРОЙКИ ПО УМОЛЧАНИЮ!");
			return GetDefaultInfo()[nameScene];
		}
		UpdateReputation();
		int playerLevel = 25;
		int countConqueredDistrict = GetCountConqueredDistrict();
		return GetDistrictInfo(GetSettingsDistrictForScene(nameScene), playerLevel, countConqueredDistrict, false);
	}

	private ReputationMapInfo GetDistrictInfo(SaveCityDistrict s, int playerLevel, int counConqueredDistrict, bool resetFlags)
	{
		ReputationMapInfo result = default(ReputationMapInfo);
		DistrictCashState districtCashState = GetDistrictCashState(s);
		result.elite = s.elite;
		result.learning = s.learning;
		int num = (s.elite ? GetEliteLevel(s, counConqueredDistrict) : 0);
		result.lockedInTutor = false;
		if ((s.elite && num < 0) || (!s.elite && s.levelAvailability > playerLevel) || result.lockedInTutor)
		{
			result.state = DistrictState.Locked;
		}
		else if (DistrictIsConquered(s))
		{
			result.state = DistrictState.Captured;
		}
		else
		{
			result.state = DistrictState.Unlocked;
		}
		if (districtCashState == DistrictCashState.cacheSpawns || districtCashState == DistrictCashState.ready)
		{
			result.spawnCash = true;
			if (districtCashState == DistrictCashState.cacheSpawns)
			{
				result.timeToSpawnMoney = (float)GetStartSpawnTime(s) + s.timeSpawnCash - (float)curApproximatedTime;
			}
			else
			{
				result.timeToSpawnMoney = 0f;
			}
			if (s.elite)
			{
				if (num >= 0)
				{
					result.cashCount = s.cashCountList[num] + (int)GetAddGoldValueForDistrict(s);
					result.cashWitoutAdditoinal = s.cashCountList[num];
				}
				else
				{
					result.cashCount = 0;
				}
			}
			else
			{
				result.cashCount = s.cashCount + (int)GetAddCashValueForDistrict(s);
				result.cashWitoutAdditoinal = s.cashCount;
			}
		}
		else
		{
			result.spawnCash = false;
			result.cashCount = 0;
		}
		result.timeSpawnMoney = s.timeSpawnCash;
		result.nextSpawnGold = GetNextSpawnGold(s);
		if (result.nextSpawnGold)
		{
			result.nextSpawnGoldCount = s.nextSpawnGoldCount + (int)GetAddGoldValueForDistrict(s);
		}
		result.curReputation = GetReputationForDistrict(s);
		result.maxReputation = s.maxReputation;
		result.requiredLevel = s.levelAvailability;
		result.name = s.name;
		result.sceneName = s.sceneName;
		result.needCaptureToUnlock = 0;
		if (result.elite && num < 0)
		{
			result.needCaptureToUnlock = s.numberToUnlockList[0] - counConqueredDistrict;
		}
		result.reputationChangedAmount = GetRepChangedAmount(s);
		if (resetFlags && result.reputationChangedAmount != 0f)
		{
			SetRepChangedAmount(s, 0f);
		}
		result.wasCaptured = GetHaveBeenCaptured(s);
		if (resetFlags && result.wasCaptured)
		{
			SetHaveBeenCaptured(s, false);
		}
		result.wasLosted = GetHaveBeenLosted(s);
		if (resetFlags && result.wasLosted)
		{
			SetHaveBeenLosted(s, false);
			result.wasCaptured = false;
		}
		result.wasUnlocked = BCWStorage.Get<int>("ReputationInfo_" + result.name) == 1 && result.state == DistrictState.Unlocked;
		result.wasLocked = BCWStorage.Get<int>("ReputationInfo_" + result.name) == 2 && result.state == DistrictState.Locked;
		if (resetFlags)
		{
			BCWStorage.Set("ReputationInfo_" + result.name, (int)result.state);
		}
		return result;
	}

	public ReputationMapInfo GetDistrictInfo(string sceneName)
	{
		SaveCityDistrict settingsDistrictForScene = GetSettingsDistrictForScene(sceneName);
		if (settingsDistrictForScene == null)
		{
			BCWDebug.LogError("District " + sceneName + " not found!");
			return default(ReputationMapInfo);
		}
		ReputationMapInfo reputationMapInfo = default(ReputationMapInfo);
		if (settingsDistrictForScene.elite)
		{
			UpdateReputation();
		}
		else
		{
			UpdateReputationDistrict(settingsDistrictForScene);
		}
		int playerLevel = 25;
		int countConqueredDistrict = GetCountConqueredDistrict();
		return GetDistrictInfo(settingsDistrictForScene, playerLevel, countConqueredDistrict, false);
	}

	private Dictionary<string, ReputationMapInfo> GetDefaultInfo()
	{
		Dictionary<string, ReputationMapInfo> dictionary = new Dictionary<string, ReputationMapInfo>();
		if (cityDistricSettings.list.Count == 0)
		{
			cityDistricSettings.LoadFromResource();
		}
		foreach (CityDistrict item in cityDistricSettings.list)
		{
			ReputationMapInfo value = default(ReputationMapInfo);
			value.state = (item.elite ? DistrictState.Locked : DistrictState.Unlocked);
			value.elite = item.elite;
			value.learning = item.learning;
			value.spawnCash = false;
			value.timeToSpawnMoney = 0f;
			value.cashCount = 0;
			value.timeSpawnMoney = 3600f;
			value.curReputation = 0f;
			value.maxReputation = 100f;
			value.requiredLevel = 0;
			value.name = item.name;
			value.needCaptureToUnlock = 0;
			value.reputationChangedAmount = 0f;
			value.wasCaptured = false;
			value.wasLosted = false;
			dictionary.Add(item.sceneName, value);
		}
		return dictionary;
	}

	public Dictionary<string, ReputationMapInfo> GetRandomInfo()
	{
		Dictionary<string, ReputationMapInfo> dictionary = new Dictionary<string, ReputationMapInfo>();
		foreach (SaveCityDistrict saveCityDistricSetting in saveCityDistricSettings)
		{
			ReputationMapInfo value = default(ReputationMapInfo);
			value.elite = ((UnityEngine.Random.Range(0f, 1f) > 0.5f) ? true : false);
			value.state = (DistrictState)UnityEngine.Random.Range(0, 4);
			if (value.state == DistrictState.Locked)
			{
				value.cashCount = 0;
				value.curReputation = 0f;
				value.timeToSpawnMoney = 0f;
				value.wasCaptured = false;
				value.spawnCash = false;
			}
			else if (value.state == DistrictState.Unlocked)
			{
				value.curReputation = UnityEngine.Random.Range(0, 80);
				value.wasCaptured = false;
				value.spawnCash = false;
				value.reputationChangedAmount = UnityEngine.Random.Range(0, (int)value.curReputation);
			}
			else
			{
				value.wasCaptured = ((UnityEngine.Random.Range(0f, 1f) > 0.5f) ? true : false);
				if (value.wasCaptured)
				{
					value.curReputation = saveCityDistricSetting.maxReputation;
					if (saveCityDistricSetting.timeSpawnCash <= 1f)
					{
						value.timeToSpawnMoney = 10800f;
					}
					else
					{
						value.timeToSpawnMoney = saveCityDistricSetting.timeSpawnCash;
					}
					value.reputationChangedAmount = UnityEngine.Random.Range(1, (int)value.curReputation);
				}
				else
				{
					value.curReputation = UnityEngine.Random.Range(80, 100);
					if (saveCityDistricSetting.timeSpawnCash <= 1f)
					{
						value.timeToSpawnMoney = ((!(UnityEngine.Random.Range(0f, 1f) > 0.5f)) ? UnityEngine.Random.Range(10, 10800) : 0);
					}
					else
					{
						value.timeToSpawnMoney = ((!(UnityEngine.Random.Range(0f, 1f) > 0.5f)) ? UnityEngine.Random.Range(10f, saveCityDistricSetting.timeSpawnCash) : 0f);
					}
					value.reputationChangedAmount = UnityEngine.Random.Range(-(int)(value.maxReputation - value.curReputation), (int)value.curReputation);
				}
				value.cashCount = saveCityDistricSetting.cashCount;
				value.spawnCash = true;
			}
			if (saveCityDistricSetting.timeSpawnCash <= 1f)
			{
				value.timeSpawnMoney = 10800f;
			}
			else
			{
				value.timeSpawnMoney = saveCityDistricSetting.timeSpawnCash;
			}
			value.maxReputation = saveCityDistricSetting.maxReputation;
			value.requiredLevel = UnityEngine.Random.Range(2, 21);
			value.needCaptureToUnlock = UnityEngine.Random.Range(1, 5);
			value.name = saveCityDistricSetting.name;
			dictionary.Add(saveCityDistricSetting.sceneName, value);
		}
		return dictionary;
	}

	private IEnumerator PreLoad()
	{
		foreach (SaveCityDistrict s in saveCityDistricSettings)
		{
			BCWStorage.Get<int>(keyPrevTimeDecreaseRep + s.name);
			DistrictCashState state = GetDistrictCashState(s);
			DistrictIsConquered(s);
			if (state == DistrictCashState.cacheSpawns)
			{
				GetStartSpawnTime(s);
			}
			GetReputationForDistrict(s);
			GetRepChangedAmount(s);
			GetHaveBeenCaptured(s);
			GetHaveBeenLosted(s);
			yield return null;
		}
	}

	public void Create()
	{
	}

	public float? CollectMoneyFromDistrict(string sceneName)
	{
		if (!JsonFileLoaded)
		{
			return null;
		}
		SaveCityDistrict settingsDistrictForScene = GetSettingsDistrictForScene(sceneName);
		if (settingsDistrictForScene == null)
		{
			BCWDebug.LogError("District " + sceneName + " not found!");
		}
		if (settingsDistrictForScene.elite)
		{
			UpdateReputation();
		}
		else
		{
			UpdateReputationDistrict(settingsDistrictForScene);
		}
		if (GetDistrictCashState(settingsDistrictForScene) == DistrictCashState.ready)
		{
			if (settingsDistrictForScene.elite)
			{
				int eliteLevel = GetEliteLevel(settingsDistrictForScene, GetCountConqueredDistrict());
				if (eliteLevel >= 0)
				{
					settings.CathedWarnings += settingsDistrictForScene.cashCountList[eliteLevel] + (int)GetAddGoldValueForDistrict(settingsDistrictForScene);
				}
			}
			else if (GetNextSpawnGold(settingsDistrictForScene))
			{
				settings.CathedWarnings += settingsDistrictForScene.nextSpawnGoldCount + (int)GetAddGoldValueForDistrict(settingsDistrictForScene);
			}
			else
			{
				settings.CathedExeptions += settingsDistrictForScene.cashCount + (int)GetAddCashValueForDistrict(settingsDistrictForScene);
			}
			SetAddGoldValueForDistrict(settingsDistrictForScene, 0f);
			SetAddCashValueForDistrict(settingsDistrictForScene, 0f);
			AddReputation(settingsDistrictForScene, (0f - saveDecreaseReputationForCollectingCache) * (float)settingsDistrictForScene.maxReputation);
			if (DistrictIsConquered(settingsDistrictForScene))
			{
				StartSpawnCash(settingsDistrictForScene);
				return settingsDistrictForScene.timeSpawnCash;
			}
			SetDistrictCashState(settingsDistrictForScene, DistrictCashState.conditionsAreNotMet);
			return null;
		}
		return null;
	}

	public bool HaveCurrencyForCollection()
	{
		UpdateReputation();
		int num = 0;
		foreach (SaveCityDistrict saveCityDistricSetting in saveCityDistricSettings)
		{
			if (GetDistrictCashState(saveCityDistricSetting) == DistrictCashState.ready)
			{
				num = ((!saveCityDistricSetting.elite) ? (num + saveCityDistricSetting.cashCount) : (num + saveCityDistricSetting.cashCountList[0]));
			}
		}
		return num > 0;
	}

	private void OnApplicationPause(bool pauseStatus)
	{
		if (pauseStatus)
		{
			ScheduleNotification();
			if (countMapOpened > 0)
			{
				EventDistricMapOpen(countMapOpened);
			}
			countMapOpened = 0;
		}
		else
		{
			RemoveNotification();
		}
	}

	private void ScheduleNotification()
	{
		string termTranslation = LocalizationManager.GetTermTranslation("PVP_map_info/lb_title_notifi");
		string termTranslation2 = LocalizationManager.GetTermTranslation("PVP_map_info/lb_text_notifi");
		int num = 0;
		for (int i = 0; i < maxNumberNotification; i++)
		{
			float num2 = 0f;
			bool flag = false;
			foreach (SaveCityDistrict saveCityDistricSetting in saveCityDistricSettings)
			{
				if (GetDistrictCashState(saveCityDistricSetting) != DistrictCashState.cacheSpawns)
				{
					continue;
				}
				float num3 = (float)GetStartSpawnTime(saveCityDistricSetting) + saveCityDistricSetting.timeSpawnCash - (float)curApproximatedTime;
				if (!(num3 < (float)num))
				{
					if (!flag)
					{
						num2 = num3;
					}
					if (num3 < num2)
					{
						num2 = num3;
					}
					flag = true;
				}
			}
			if (flag)
			{
				int num4 = Mathf.Max(num + minTimeToNotify, (int)num2);
				int item = EtceteraAndroid.scheduleNotification(num4, termTranslation, termTranslation2, string.Empty, string.Empty);
				_notificationIds.Add(item);
				num = num4;
				continue;
			}
			break;
		}
	}

	private void RemoveNotification()
	{
		foreach (int notificationId in _notificationIds)
		{
			EtceteraAndroid.cancelNotification(notificationId);
		}
	}

	public bool JsonIsLoad()
	{
		return JsonFileLoaded;
	}

	public void DebugSetReputation(float newValue, string name)
	{
		SaveCityDistrict district = saveCityDistricSettings.Find((SaveCityDistrict d) => d.name.Equals(name));
		AddReputation(district, newValue - GetReputationForDistrict(district));
	}

	public void DebugSetMaxReputation(string sceneName)
	{
	}

	public void DebugSetCashIsSpawn(string sceneName)
	{
	}

	public static int GetUnixTime()
	{
		return (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
	}
}
